package com.droid.lib.controls.demo.view;

import java.util.Calendar;
import java.util.Random;

import com.droid.lib.controls.demo.R;
import com.droid.lib.controls.demo.util.MResource;
import com.droid.lib.controls.widget.RippleImageView;
import com.droid.lib.controls.widget.calendar.CalendarDay;
import com.droid.lib.controls.widget.calendar.CalendarMode;
import com.droid.lib.controls.widget.calendar.MaterialCalendarView;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.NumberPicker;

public class CalendarActivity extends FragmentActivity {
	private MaterialCalendarView calendarView;
	private RippleImageView btn_back;
	private Random random = new Random();
	private int currentTileSize;
	private int currentTileWidth;
	private int currentTileHeight;
	private static final int[] DAYS_OF_WEEK = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY,
			Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY, };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calendar);
		int type = getIntent().getIntExtra("type", 0);
		calendarView = (MaterialCalendarView) findViewById(R.id.calendarView);
		calendarView.setHeaderTextColor(Color.BLACK);
		calendarView.setHeaderTextSize(MResource.getSP(CalendarActivity.this, 7));
		btn_back = (RippleImageView) findViewById(R.id.btn_back);
		btn_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});
		currentTileSize = MaterialCalendarView.DEFAULT_TILE_SIZE_DP;
		currentTileWidth = MaterialCalendarView.DEFAULT_TILE_SIZE_DP;
		currentTileHeight = MaterialCalendarView.DEFAULT_TILE_SIZE_DP;
		((Button) findViewById(R.id.button_other_dates)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				CharSequence[] items = { "Other Months", "Out Of Range", "Decorated Disabled",
						"Select days outside month" };
				final int[] itemValues = { MaterialCalendarView.SHOW_OTHER_MONTHS,
						MaterialCalendarView.SHOW_OUT_OF_RANGE, MaterialCalendarView.SHOW_DECORATED_DISABLED, };
				int showOtherDates = calendarView.getShowOtherDates();

				boolean[] initSelections = { MaterialCalendarView.showOtherMonths(showOtherDates),
						MaterialCalendarView.showOutOfRange(showOtherDates),
						MaterialCalendarView.showDecoratedDisabled(showOtherDates),
						calendarView.allowClickDaysOutsideCurrentMonth() };
				new AlertDialog.Builder(CalendarActivity.this).setTitle("Show Other Dates")
						.setMultiChoiceItems(items, initSelections, new DialogInterface.OnMultiChoiceClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which, boolean isChecked) {
								if (which < 3) {
									int showOtherDates = calendarView.getShowOtherDates();
									if (isChecked) {
										// Set flag
										showOtherDates |= itemValues[which];
									} else {
										// Unset flag
										showOtherDates &= ~itemValues[which];
									}
									calendarView.setShowOtherDates(showOtherDates);
								} else if (which == 3) {
									calendarView.setAllowClickDaysOutsideCurrentMonth(isChecked);
								}
							}
						}).setPositiveButton(android.R.string.ok, null).show();

			}
		});
		((CheckBox) findViewById(R.id.check_text_appearance)).setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					// calendarView.setHeaderTextAppearance(R.style.TextAppearance_AppCompat_Large);
					// calendarView.setDateTextAppearance(R.style.TextAppearance_AppCompat_Medium);
					// calendarView.setWeekDayTextAppearance(R.style.TextAppearance_AppCompat_Medium);
					calendarView.setHeaderTextAppearance(android.R.color.black);
					calendarView.setDateTextAppearance(android.R.color.black);
					calendarView.setWeekDayTextAppearance(android.R.color.black);
				} else {
					calendarView.setHeaderTextAppearance(R.style.TextAppearance_MaterialCalendarWidget_Header);
					calendarView.setDateTextAppearance(R.style.TextAppearance_MaterialCalendarWidget_Date);
					calendarView.setWeekDayTextAppearance(R.style.TextAppearance_MaterialCalendarWidget_WeekDay);
				}
				calendarView
						.setShowOtherDates(isChecked ? MaterialCalendarView.SHOW_ALL : MaterialCalendarView.SHOW_NONE);
			}
		});
		((CheckBox) findViewById(R.id.check_page_enabled)).setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				calendarView.setPagingEnabled(isChecked);

			}
		});
		((Button) findViewById(R.id.button_previous)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				calendarView.goToPrevious();

			}
		});
		((Button) findViewById(R.id.button_next)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				calendarView.goToNext();

			}
		});
		((Button) findViewById(R.id.button_min_date)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDatePickerDialog(CalendarActivity.this, calendarView.getMinimumDate(),
						new DatePickerDialog.OnDateSetListener() {
							@Override
							public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
								calendarView.state().edit()
										.setMinimumDate(CalendarDay.from(year, monthOfYear, dayOfMonth)).commit();
							}
						});
			}
		});
		((Button) findViewById(R.id.button_max_date)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDatePickerDialog(CalendarActivity.this, calendarView.getMinimumDate(),
						new DatePickerDialog.OnDateSetListener() {
							@Override
							public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
								calendarView.state().edit()
										.setMaximumDate(CalendarDay.from(year, monthOfYear, dayOfMonth)).commit();
							}
						});
			}
		});
		((Button) findViewById(R.id.button_selected_date)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDatePickerDialog(CalendarActivity.this, calendarView.getSelectedDate(),
						new DatePickerDialog.OnDateSetListener() {
							@Override
							public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
								calendarView.setSelectedDate(CalendarDay.from(year, monthOfYear, dayOfMonth));
							}
						});
			}
		});
		((Button) findViewById(R.id.button_toggle_topbar)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				calendarView.setTopbarVisible(!calendarView.getTopbarVisible());
			}
		});
		((Button) findViewById(R.id.button_set_colors)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int color = Color.HSVToColor(new float[] { random.nextFloat() * 360, 1f, 0.75f });
				calendarView.setArrowColor(color);
				calendarView.setSelectionColor(color);
			}
		});
		((Button) findViewById(R.id.button_set_tile_size)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final NumberPicker view = new NumberPicker(CalendarActivity.this);
				view.setMinValue(24);
				view.setMaxValue(64);
				view.setWrapSelectorWheel(false);
				view.setValue(currentTileSize);
				new AlertDialog.Builder(CalendarActivity.this).setView(view)
						.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								currentTileSize = view.getValue();
								calendarView.setTileSizeDp(currentTileSize);
							}
						}).show();
			}
		});
		((Button) findViewById(R.id.button_set_width_height)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final LinearLayout layout = new LinearLayout(CalendarActivity.this);
				layout.setOrientation(LinearLayout.HORIZONTAL);
				final NumberPicker pickerWidth = new NumberPicker(CalendarActivity.this);
				pickerWidth.setMinValue(24);
				pickerWidth.setMaxValue(64);
				pickerWidth.setWrapSelectorWheel(false);
				pickerWidth.setValue(currentTileWidth);
				final NumberPicker pickerHeight = new NumberPicker(CalendarActivity.this);
				pickerHeight.setMinValue(24);
				pickerHeight.setMaxValue(64);
				pickerHeight.setWrapSelectorWheel(false);
				pickerHeight.setValue(currentTileHeight);
				layout.addView(pickerWidth);
				layout.addView(pickerHeight);
				new AlertDialog.Builder(CalendarActivity.this).setView(layout)
						.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								currentTileWidth = pickerWidth.getValue();
								currentTileHeight = pickerHeight.getValue();
								calendarView.setTileSize(-1);
								calendarView.setTileWidthDp(currentTileWidth);
								calendarView.setTileHeightDp(currentTileHeight);
							}
						}).show();
			}
		});
		((Button) findViewById(R.id.button_clear_selection)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				calendarView.clearSelection();
			}
		});
		((Button) findViewById(R.id.button_selection_mode)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				CharSequence[] items = { "No Selection", "Single Date", "Multiple Dates", "Range of Dates" };
				new AlertDialog.Builder(CalendarActivity.this).setTitle("Selection Mode").setSingleChoiceItems(items,
						calendarView.getSelectionMode(), new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								calendarView.setSelectionMode(which);
								dialog.dismiss();
							}
						}).show();
			}
		});
		((Button) findViewById(R.id.button_set_first_day)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int index = random.nextInt(DAYS_OF_WEEK.length);
				calendarView.state().edit().setFirstDayOfWeek(DAYS_OF_WEEK[index]).commit();
			}
		});
		((Button) findViewById(R.id.button_weeks)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				calendarView.state().edit().setCalendarDisplayMode(CalendarMode.WEEKS).commit();
			}
		});
		((Button) findViewById(R.id.button_months)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				calendarView.state().edit().setCalendarDisplayMode(CalendarMode.MONTHS).commit();
			}
		});
	}

	public static void showDatePickerDialog(Context context, CalendarDay day,
			DatePickerDialog.OnDateSetListener callback) {
		if (day == null) {
			day = CalendarDay.today();
		}
		DatePickerDialog dialog = new DatePickerDialog(context, 0, callback, day.getYear(), day.getMonth(),
				day.getDay());
		dialog.show();
	}
}
